package com.lcredu.springboot.comtroller;

import com.lcredu.springboot.entity.Monster;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author 黎长容
 * @date 2025-08-11 20:05
 */
@Controller
@Slf4j
public class RequestController {

    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        request.setAttribute("name", "黎长容");
        request.getSession().setAttribute("age", 23);
        return "forward:/ok";
    }

    @GetMapping("/ok")
    @ResponseBody
    public String ok(@RequestAttribute(value = "name", required = false) String username,
                     @SessionAttribute(value = "age", required = false) Integer age) {
        System.out.println("username=" + username);
        System.out.println("age=" + age);

        return "success";
    }


    @RequestMapping("/register")
    public String register(HttpServletResponse response,
                           Map<String, Object> map,
                           Model model) {
        map.put("user", "lcredu");
        map.put("job", "Java工程师");
        model.addAttribute("sal", 10200);
        //创建cookie,添加到客户端
        Cookie cookie = new Cookie("email", "lcr@163.com");
        response.addCookie(cookie);
        return "forward:/registerOk";
    }

    @ResponseBody
    @GetMapping("/registerOk")
    public String registerOk(HttpServletRequest request) {
        System.out.println("request 域中 user= " + request.getAttribute("user"));
        System.out.println("request 域中 job= " + request.getAttribute("job"));
        System.out.println("request 域中 sal= " + request.getAttribute("sal"));
        return "success";
    }

    @PostMapping("/savemonster")
    @ResponseBody
    public String saveMonster(Monster monster) {
        log.info("这是monster={}", monster);
        return "success";
    }
    
    
}
